package com.lovo.view.info;

import com.lovo.bean.UserBean;
import com.lovo.javafxComponent.LovoButton;
import com.lovo.javafxComponent.LovoDialog;
import com.lovo.javafxComponent.LovoPassword;
import com.lovo.javafxComponent.LovoTitle;
import com.lovo.service.IUserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.view.LoginView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;

/**
 * 修改密码面板
 * @author lovo
 *
 */
public class UpdatePwdPane extends Pane {
	/**根容器*/
	private BorderPane root;
	/**登陆用户*/
	private UserBean loginUser;
	/**旧密码框*/
	private LovoPassword oldPwdTxt = new LovoPassword("请输入旧密码", 200, 200, this);
	/**新密码框*/
	private LovoPassword newPwdTxt = new LovoPassword("请输入新密码", 200, 280, this);
	/**确认密码框*/
	private LovoPassword rePwdTxt = new LovoPassword("请确认新密码", 200, 360, this);
	
	private IUserService userService = new UserServiceImpl();
	
	public UpdatePwdPane(BorderPane root,UserBean loginUser) {
		this.loginUser = loginUser;
		this.root = root;
		this.initComponent();
	}
	/**
	 * 初始化组件
	 */
	private void initComponent() {
		new LovoTitle("修改密码",0,80,this);
		
		LovoButton updateButton = new LovoButton("修改", 320, 440, this);
		updateButton.setOnAction(e -> updatePwd());
	}
	
	//----------------------
	/**
	 * 修改密码
	 */
	private void updatePwd() {
		// 旧密码的验证
		if(!oldPwdTxt.getText().equals(loginUser.getPassword())) {
			LovoDialog.showMessageDialog("旧密码输入有误");
			return;
		}
		// 输入密码为空的验证
		if(newPwdTxt.getText().equals("") || rePwdTxt.getText().equals("")) {
			LovoDialog.showMessageDialog("密码不能为空！");
			return;
		}
		// 输入密码不同的验证
		if(!newPwdTxt.getText().equals(rePwdTxt.getText())) {
			LovoDialog.showMessageDialog("两次密码输入不一致！");
			return;
		}
		
		userService.updatePwd(loginUser.getId(), newPwdTxt.getText());
		
		root.getChildren().clear();
		new LoginView(root);
	}
}
